/*
 * @Descripttion: 
 * @Author: junhuafly
 * @Date: 2020-02-26 15:59:57
 * @LastEditors: junhuafly
 * @Email: junhuafly@163.com
 * @LastEditTime: 2020-04-21 11:15:01
 */
// pages/note/index.js
const app = getApp()
import {
  noteAPI,
  globalAPI
} from "./../../../api/index"

import {
  returnStatus
} from '../../../utils/util'

const api = {
  find: '/note/discovery',
  recommend: '/note/recommendation'
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    initImg: '/pages/images/1.jpg',
    hasNewMsg: false,
    value: '',
    isResult: false,
    showReadPopup: false,
    open: false,
    wait: false,
    notes: [],
    pageNum: 1,
    pages: 1,
    activeTab: 'find',
    api: api.find,
    requestObj: {},
    nvabarData: {
      showCapsule: 1, //是否显示左上角图标   1表示显示    0表示不显示
    },
  },

  toSearch(){
    wx.redirectTo({
      url: '/pages/search/index',
    })
  },
  closeReadPopup() {
    this.setData({
      showReadPopup: false
    })
  },

  onOpenRead() {
    this.setData({
      open: true
    })
  },

  handOpen() {
    this.setData({
      wait: true
    })
    setTimeout(() => {
      this.setData({
        open: true
      })
    }, 500);
  },

  toSearch() {
    wx.navigateTo({
      url: '/pages/search/index',
    });
  },
  toDetail({
    currentTarget
  }) {
    const {
      noteid
    } = currentTarget.dataset

    wx.navigateTo({
      url: `detail/index?noteId=${noteid}`,
    })
  },
  toMsg() {
    wx.navigateTo({
      url: '/pages/mine/msg/index',
      success: (result) => {
        result.eventChannel.emit('props', {
          type: 'info'
        })
      },
      fail: () => {},
      complete: () => {}
    });
  },
  // 请求数据
  getData(pageSize = 10) {
    const {
      key,
      pageNum,
      api: url
    } = this.data
    return new Promise((resolve, reject) => {
      const getRes = key ? globalAPI.queryNoteByKey({
        keyword: key,
        pageNum,
        pageSize
      }) : noteAPI.getNotes({
        pageNum,
        url
      })
      getRes.then(res => {
        if (res.code === 200) {
          this.setData({
            notes: [...this.data.notes, ...res.data.records],
            pageNum: ++this.data.pageNum,
            pages: res.data.records.length
          })
          resolve(this.data.pages)
        } else {
          reject(false)
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function ({key}) {
    const isIphoneX = app.globalData.isIphoneX
    this.setData({
      key,
      isIphoneX,
      width:app.globalData.width,
      height: app.globalData.height,
      isResult: key != null,
      requestObj: {
        fetchData: (refresh) => {
          refresh ? this.setData({
            pageNum: 1,
            notes: []
          }) : ''
          return this.getData()
        },
      }
    })
    setTimeout(() => {
      this.getData()
    }, 500);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    delete app.data.key
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})